package com.summer.Thread;
/**
多线程创建方式一:
 1.创建一个继承Thread类的子类
 2.重写Thread类的run() --> 将此线程执行的操作声明在run()中
 3.创建Thread类的子类对象
 4.通过此对象调用start()
 */
class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            yield();//释放当前cpu资源
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (i%2==0) System.out.println(i+"----------"+Thread.currentThread().getName());
        }
    }
}
public class ThreadTest{
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.setName("线程1--遍历偶数");
        System.out.println("线程1的优先级："+myThread.getPriority());
        myThread.setPriority(10);
        System.out.println("线程1的优先级："+myThread.getPriority());
        myThread.start();

        for (int i = 0; i < 100; i++) {
            System.out.println(i+"----------"+Thread.currentThread().getName());
        }

        //匿名函数的方式
        new Thread("线程2---遍历奇数"){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i == 50) {
                        try {
                           myThread.join();//在线程A中调用线程b的join()，此时线程A就进入阻塞状态,直到线程B执行完成
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (i%2!=0)
                    System.out.println(i+"----------"+Thread.currentThread().getName());
                }
            }
        }.start();
    }
}
